<?php

namespace app\api\controller\cart;

use app\api\controller\AuthController;
use app\api\service\CartService;
use app\api\service\CommissionService;
use app\api\service\OrderService;
use think\exception\ValidateException;
use think\facade\Db;

class CartController extends AuthController
{

    /**
     * 测试
     */
    // public function test()
    // {
    //     $c = new CommissionService();
    //     $c->fun();
    // }

    /**
     * 购物车结算商品
     */
    public function settlementCart()
    {
        $param = $this->request->param();
        $user_id = $this->request->user['user_id'];
        if (empty($param['store_id'])) {
            return json(['code' => 0, 'msg' => '非法请求']);
        }
        $cart_ids = empty($param['cart_ids']) ? '' : $param['cart_ids'];

        $list = CartService::cartList($param, $user_id, $cart_ids);
        list($l, $allPrice, $c, $m, $d, $dd) = CartService::cartTotal($param, $user_id, $cart_ids);
        if ($c == 1) {
            return json(['code' => 1, 'data' => ['goods_list' => $list, 'total_price' => $allPrice], 'msg' => '查询成功']);
        } else {
            return json(['code' => 0, 'data' => [], 'msg' => $m]);
        }

    }

    /**
     * 我的购物车列表
     */
    public function cartList()
    {
        $param = $this->request->param();
        $user_id = $this->request->user['user_id'];
        if (empty($param['store_id'])) {
            return json(['code' => 0, 'msg' => '非法请求']);
        }
        $list = CartService::cartList($param, $user_id);
        list($l, $allPrice, $c, $m, $d, $dd) = CartService::cartTotal($param, $user_id);
        return json(['code' => 1, 'cart_total' => $allPrice, 'data' => $list, 'msg' => '查询成功']);
    }

    /**
     * 全部删除购物车
     */
    public function allDelCart()
    {
        $param = $this->request->param();
        $user_id = $this->request->user['user_id'];
        if (empty($param['store_id'])) {
            return json(['code' => 0, 'msg' => '非法请求']);
        }
        $rest = Db::name('cart')->where(
            ['store_id' => $param['store_id'], 'uid' => $user_id, 'delete_time' => 0]
        )->find();

        if (empty($rest)) {
            return json(['code' => 0, 'msg' => '购物车为空不能删除']);
        }

        $rst = CartService::delCart($param, $user_id, 1);
        return $rst ? json(['code' => 1, 'msg' => '删除成功']) : json(['code' => 0, 'msg' => '删除失败']);
    }

    /**
     * 单个删除购物车
     */
    public function singleDelCart()
    {
        $param = $this->request->param();
        $user_id = $this->request->user['user_id'];
        try {
            validate('CartValidate')->scene('del')->check($param);
        } catch (ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        $rest = Db::name('cart')->where(
            ['store_id' => $param['store_id'], 'uid' => $user_id, 'delete_time' => 0]
        )->where('id', 'in', $param['cart_id'])->find();

        if (empty($rest)) {
            return json(['code' => 0, 'msg' => '商品不存在']);
        }

        $rst = CartService::delCart($param, $user_id);
        return $rst ? json(['code' => 1, 'msg' => '删除成功']) : json(['code' => 0, 'msg' => '删除失败']);
    }

    /**
     * 改变数量
     */
    public function modifyNum()
    {
        $param = $this->request->param();
        $user_id = $this->request->user['user_id'];
        $num = empty($param['num']) ? 1 : $param['num'];
        try {
            validate('CartValidate')->scene('modify')->check($param);
        } catch (ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        $rest = Db::name('cart')->where(
            ['store_id' => $param['store_id'], 'uid' => $user_id, 'delete_time' => 0, 'id' => $param['cart_id']]
        )->find();

        if (empty($rest)) {
            return json(['code' => 0, 'msg' => '商品不存在']);
        }

        if ($param['type'] != 1 && $rest['buy_num'] <= $num) {
            return json(['code' => 0, 'msg' => '商品数量不能低于一个']);
        }
        $rst = CartService::modifyNum($param, $num);
        return $rst ? json(['code' => 1, 'msg' => '修改成功']) : json(['code' => 0, 'msg' => '修改失败']);
    }

    /**
     * 添加购物车
     */
    public function addCart()
    {
        $param = $this->request->param();
        try {
            validate('CartValidate')->scene('add')->check($param);
        } catch (ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        $goods = OrderService::findSingleGoods($param['goods_id']);
        if (empty($goods)) {
            return json(['code' => 0, 'msg' => '商品已下架']);
        }
        if ($goods['spec_type'] != 1 && empty($param['sku_id'])) {
            return json(['code' => 0, '商品规格有误']);
        }
        if (!empty($param['sku_id'])) {
            $goods = OrderService::findManyGoods($param['sku_id'], $param['goods_id']);
        } else {
            $param['sku_id'] = 0;
        }

        $param['buy_num'] = isset($data['buy_num']) ? $data['buy_num'] : 1;
        if ($goods['stock'] < $param['buy_num']) {
            return json(['code' => 0, 'msg' => '库存不足']);
        }
        $user_id = $this->request->user['user_id'];
        $rst = CartService::add($param, $goods, $user_id);
        return $rst ? json(['code' => 1, 'msg' => '添加成功']) : json(['code' => 0, 'msg' => '添加失败']);
    }
}
